//请实现一个函数，用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样，那么它是对称的。 
//
// 例如，二叉树 [1,2,2,3,4,4,3] 是对称的。 
//
// 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 
//
// 1 / \ 2 2 \ \ 3 3 
//
// 
//
// 示例 1： 
//
// 输入：root = [1,2,2,3,4,4,3]
//输出：true
// 
//
// 示例 2： 
//
// 输入：root = [1,2,2,null,3,null,3]
//输出：false 
//
// 
//
// 限制： 
//
// 0 <= 节点个数 <= 1000 
//
// 注意：本题与主站 101 题相同：https://leetcode-cn.com/problems/symmetric-tree/ 
//
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 449 👎 0


package com.leetcode.editor.cn;

import com.leetcode.editor.cn.common.TreeNode;

import java.util.LinkedList;
import java.util.Objects;
import java.util.Queue;

public class PJianZhiOffer28DuiChengDeErChaShuLcof {
    public static void main(String[] args) {
        Solution solution = new PJianZhiOffer28DuiChengDeErChaShuLcof().new Solution();
        TreeNode root = new TreeNode(1);
        TreeNode l1 = new TreeNode(2);
        TreeNode r1 = new TreeNode(2);
        root.left = l1;
        root.right = r1;

//        TreeNode l1l = new TreeNode(3);
        TreeNode l1r = new TreeNode(4);
        l1.left = null;
        l1.right = l1r;
        TreeNode r1l = new TreeNode(4);
//        TreeNode r1r = new TreeNode(3);
        r1.left = r1l;
        r1.right = null;
        System.out.println(solution.isSymmetric(root));
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSymmetric(TreeNode root) {
//            return cyle(root);
            return symmetric(root, root);
        }

        private boolean symmetric(TreeNode left, TreeNode right) {
            if (left == null && right == null) {
                return true;
            }
            if (left == null || right == null) {
                return false;
            }
            return left.val == right.val && symmetric(left.left, right.right) && symmetric(left.right, right.left);
        }

        private boolean symmetric(TreeNode root) {
            if (root == null) return true;
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            queue.offer(root);
            while (!queue.isEmpty()) {
                TreeNode left = queue.poll();
                TreeNode right = queue.poll();
                if (left == null && right == null) {
                    continue;
                }
                if ((left == null || right == null) || (!Objects.equals(left.val, right.val))) {
                    return false;
                }
                queue.offer(left.left);
                queue.offer(right.right);
                queue.offer(left.right);
                queue.offer(right.left);
            }
            return true;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}